package com.zjx.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.reggie.common.R;
import com.zjx.reggie.domain.Employee;
import com.zjx.reggie.service.Impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/employee")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl es;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1.将页面提交的密码进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> lqy = new LambdaQueryWrapper<>();
        lqy.eq(Employee::getUsername, employee.getUsername());
        //2.1通过用户名查询数据库中是否存在该用户；
        Employee esOne = es.getOne(lqy);
        if (esOne == null) {
            R.error("登录失败");
        }
        //2.2如果通过username查询返回的结果不是null，就进行密码验证；
        if (!employee.getPassword().equals(esOne.getPassword())) {
            R.error("登录失败，密码错误");
            //2.3如果密码也正确，实则可以登录，但是离职的员工，不能登录，所以要符合事实，在通过员工的状态判断是否登录；以上三个条件都需满足，才允许登录；
        }
        if (esOne.getStatus() == 0) {
            R.error("登录失败，你已离职");
        }
        //2.4恭喜登录成功，登录成功，将员工id存入session中并返回结果；
        request.getSession().setAttribute("employee", esOne.getId());
        return R.success(esOne);
    }

    //员工点击退出按钮，前端会通过异步发送一个json格式的异步请求，，大致逻辑是将seesion中存储的id，中域中移除，结束请求之间的数据共享；

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");

    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工信息", employee.toString());
        //设置初始密码，使用md5加密算法
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置更新时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //设置当前创建用户的id
//        Long creatUser = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(creatUser);
//        employee.setUpdateUser(creatUser);

        es.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页或分页条件查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        //构造分页构造器
        Page page1 = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(name), Employee::getUsername, name);

        //添加排序条件
        queryWrapper.orderByDesc(Employee::getCreateTime);
        Page pageMaps = es.pageMaps(page1, queryWrapper);
        return R.success(pageMaps);
    }

    /**
     * 修改员工信息，1。修改员工状态，2.修改员工具体信息，将修改人和修改时间进行更新；
     *
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
//        Long emId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(emId);
//        employee.setUpdateTime(LocalDateTime.now());
//        es.updateById(employee);
        return R.success("员工信息更新成功");
    }

    @GetMapping("/{id}")
    public R<Employee> employeeById(@PathVariable Long id) {
        Employee employee = es.getById(id);
        if (employee == null) {

            return R.error("暂无员工信息");

        } else {
            return R.success(employee);
        }
    }


}
